#include "CameraSystem.h"
#include "Base/Logger.h"
#include "Base/Exceptions.h"
#include "PositionComponent.h"

CameraSystem::CameraSystem():mpt_mode(CameraMode::PLAYER), mpt_moveVector(sf::Vector2f(0.0f,0.0f)), mpt_cameraSpeed(20.0f)
{
#ifdef EDITOR_MODE
	mpt_mode = CameraMode::FREE;
#endif
}
void CameraSystem::update(float dt)
{
    Assert(mpt_app != NULL);

	if(mpt_mode == CameraMode::FREE)
	{
		if(sf::Keyboard::isKeyPressed(sf::Keyboard::Q))
		{
			mpt_moveVector += sf::Vector2f(-1.0f,0.0f);
		}
		if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
		{
			mpt_moveVector += sf::Vector2f(1.0f,0.0f);
		}
		if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
		{
			mpt_moveVector += sf::Vector2f(0.0f,1.0f);
		}
		if(sf::Keyboard::isKeyPressed(sf::Keyboard::Z))
		{
			mpt_moveVector += sf::Vector2f(0.0f,-1.0f);
		}
		if(dt>0.03)
			dt = 0.03;

		sf::Vector2f movement = dt * mpt_cameraSpeed * mpt_moveVector;
		sf::Vector2f posCenter(int(mpt_view.getCenter().x + movement.x), int(mpt_view.getCenter().y + movement.y));
		mpt_view.setCenter(posCenter);
		mpt_app->setView(mpt_view);

		mpt_moveVector = sf::Vector2f(0.0f,0.0f);

	}
	else if(mpt_mode == CameraMode::PLAYER)
	{
		mpt_moveVector = entityManager->getEntity(mpt_playerID)->getAs<PositionComponent>()->m_position - mpt_view.getCenter();
		if(mpt_moveVector.x * mpt_moveVector.x + mpt_moveVector.y * mpt_moveVector.y < 2)
			mpt_moveVector = sf::Vector2f(0.0f,0.0f);
		sf::Vector2f movement = dt * mpt_cameraSpeed * mpt_moveVector;
		sf::Vector2f posCenter(int(mpt_view.getCenter().x + movement.x), int(mpt_view.getCenter().y + movement.y));
		mpt_view.setCenter(posCenter);

		if(mpt_view.getCenter().x < mpt_view.getSize().x/2)
		{	
			sf::Vector2f center = mpt_view.getCenter();
			center.x = mpt_view.getSize().x/2;
			mpt_view.setCenter(center);
		}
		if(mpt_view.getCenter().y < mpt_view.getSize().y/2)
		{	
			sf::Vector2f center = mpt_view.getCenter();
			center.y = mpt_view.getSize().y/2;
			mpt_view.setCenter(center);
		}
		if(mpt_view.getCenter().x > entityManager->getMap()->getSize().x * entityManager->sizeTile - mpt_view.getSize().x/2)
		{	
			sf::Vector2f center = mpt_view.getCenter();
			center.x = entityManager->getMap()->getSize().x * entityManager->sizeTile - mpt_view.getSize().x/2;
			mpt_view.setCenter(center);
		}
		if(mpt_view.getCenter().y > entityManager->getMap()->getSize().y * entityManager->sizeTile -  mpt_view.getSize().y/2)
		{	
			sf::Vector2f center = mpt_view.getCenter();
			center.y = entityManager->getMap()->getSize().y * entityManager->sizeTile -  mpt_view.getSize().y/2;
			mpt_view.setCenter(center);
		}


		mpt_app->setView(mpt_view);

		mpt_moveVector = sf::Vector2f(0.0f,0.0f);
	}
}

void CameraSystem::addEntity(int ent)
{
	if(!entityManager->hasEntity(ent))
		return;

    mpt_entitiesID.insert(ent);
	entityManager->getEntity(ent)->addSystem(this);
	mpt_playerID = ent;
}

void CameraSystem::getEvent(sf::Event& event)
{
    if(event.type == sf::Event::KeyPressed)
	{
		if(mpt_mode == CameraMode::FREE)
		{
		}
	}
}

void CameraSystem::attachApp(sf::RenderWindow* app)
{
    mpt_app = app;
    Assert(mpt_app != NULL);
	mpt_view = mpt_app->getView();
	//mpt_view.SetHalfSize(entityManager->sizeTile * 12, entityManager->sizeTile * 9);
#ifdef EDITOR_MODE
	//mpt_view.zoom(0.5);
#endif
}

void CameraSystem::deleteEntity(int ent)
{
	std::set<int>::iterator itr = mpt_entitiesID.find(ent);
	if(itr != mpt_entitiesID.end())
	{
		mpt_entitiesID.erase(itr);
	}
	if(mpt_playerID == ent)
	{
		mpt_playerID = 0;
		mpt_mode = CameraMode::FREE;
	}
	BPL::Logger::log("delete from CameraSystem");
}